package com.cyan.app.workflow.fragment.lunbo;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.cyan.app.workflow.bean.BannerBean;
import com.cyan.app.workflow.util.LoadingImgUtil;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class BannerAdapter extends PagerAdapter {

	private Context mContext;
	List<BannerBean.Banner> mBannerItems;
	Map<String, BitmapDrawable> mBitmapCacheMap;

	public BannerAdapter(Context context, List<BannerBean.Banner> lunboList) {
		this.mBannerItems = lunboList;
		this.mContext = context;
		this.mBitmapCacheMap = new TreeMap<String, BitmapDrawable>();
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((ImageView) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		final int park = position % mBannerItems.size();
		final BannerBean.Banner banner = mBannerItems.get(park);
		ImageView img = new ImageView(mContext);
		img.setTag(position);
		img.setScaleType(ScaleType.FIT_XY);
		LoadingImgUtil.loadBannerImg(banner.getImageUrl(), img);
		((ViewPager) container).addView(img, 0);

		img.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		return img;
	}
}
